package org.web.accesscontrollist.server.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration("accessControlListServerDBConfig")
@MapperScan(basePackages = "org.web.accesscontrollist.server.dao", sqlSessionFactoryRef = "accessControlListServerSqlSessionFactory")
public class AccessControlListServerDBConfig {

    @Bean("accessControlListServerDataSource")
    @ConfigurationProperties(prefix = "org.web.acl.server.jdbc")
    public DataSource dogMarketDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        return dataSource;
    }

    @Bean("accessControlListServerSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("accessControlListServerDataSource") DataSource dogMarketDataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dogMarketDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/Mapper*.xml"));
        return sessionFactory.getObject();
    }
}
